//
//  ProjectListCtrl.m
//  laowu
//
//  Created by Northwan on 15/5/9.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "ProjectListCtrl.h"
#import "ProjectListCell.h"
#import "ProjectDataTool.h"
#import "TimeUtils.h"
#import "ProjectListDetailCtrl.h"
#import "RecruitmentCtrl.h"

@interface ProjectListCtrl ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,copy) NSString* keywords;
@property(nonatomic,strong) NSString* city;
@property(nonatomic,copy) NSArray* jobArr;
@property(nonatomic,strong) PublishDateModel* date;
@property(weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *projectArr;


@property(nonatomic,copy)NSMutableString* job;
@property(nonatomic,copy)NSString *start;
@property(nonatomic,copy)NSString *end;
@end

@implementation ProjectListCtrl


+(instancetype)initWithSearch:(NSString *)keywords city:(NSString *)city job:(NSArray *)job date:(PublishDateModel *)date{
    ProjectListCtrl* object = [[ProjectListCtrl alloc]init];
    object.keywords = keywords;
    object.city = city;
    object.jobArr = job;
    object.date = date;
    return object;
}

- (void)viewDidLoad {
   [super viewDidLoad];
    [self setTitle:@"职位搜索"];
    _projectArr = [NSMutableArray array];
    [self initTableView];
    [self dealWithParams];
    [_tableView headerBeginRefreshing];
}

-(void)dealWithParams{
    if (!self.hideHeader) {
        //添加table头
        UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, SCREENW, 44)];
        searchBar.placeholder = @"搜索";
        UIButton *button = [[UIButton alloc] initWithFrame:searchBar.frame];
        [button addTarget:self action:@selector(searchBarBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [searchBar addSubview:button];
        self.tableView.tableHeaderView = searchBar;
    }
    
    _job = [[NSMutableString alloc]init];
    for (int i = 0; i<_jobArr.count; i++) {
        [_job appendString:_jobArr[i]];
        if(i!=_jobArr.count-1){
            [_job appendString:@","];
        }
    }
    _end = [TimeUtils getFormatCurrentDate];
    NSDate * currentDate = [NSDate date];
    NSDate *preDate;
    switch ([_date date_id]) {
        case ALL_DAY:
            _start = @"";
            _end = @"";
            break;
        case ONE_DAY:
            preDate = [NSDate dateWithTimeInterval:-(24*60*60) sinceDate:currentDate];
            break;
        case TWO_DAY:
            preDate = [NSDate dateWithTimeInterval:-(24*60*60*2) sinceDate:currentDate];
            break;
        case THREE_DAY:
              preDate = [NSDate dateWithTimeInterval:-(24*60*60*3) sinceDate:currentDate];
            break;
        case FORE_DAY:
              preDate = [NSDate dateWithTimeInterval:-(24*60*60*4) sinceDate:currentDate];
            break;
        case ONE_WEEK:
               preDate = [NSDate dateWithTimeInterval:-(24*60*60*7) sinceDate:currentDate];;
            break;
        case TWO_WEEK:
               preDate = [NSDate dateWithTimeInterval:-(24*60*60*14) sinceDate:currentDate];
            break;
        case ONE_MONTH:
             preDate = [NSDate dateWithTimeInterval:-(24*60*60*30) sinceDate:currentDate];
            break;
        case TWO_MONTH:
               preDate = [NSDate dateWithTimeInterval:-(24*60*60*60) sinceDate:currentDate];
            break;
        default:
            break;
    }
    _start = [TimeUtils getFormatCurrentDate:preDate];
}

-(void)searchBarBtnClick{
    NSLog(@"searchBarBtnClick");
    RecruitmentCtrl *ctrl = [[RecruitmentCtrl alloc]initWithType:RecruitmentTypeJobSearch];
    [self.navigationController pushViewController:ctrl animated:YES];
}


-(void)getDataWithMaxId:(NSInteger)maxId{
    if(ISBLANK(_keywords)){
     _keywords = @"";
    }
    if(ISBLANK(_city) || [_city isEqualToString:@"全国"]){
     _city = @"";
    }
    if(ISBLANK(_job)){
        _job = [NSMutableString string];
    }
    if(ISBLANK(_start)){
     _start = @"";
    }
    if(ISBLANK(_end)){
     _end = @"";
    }
    [ProjectDataTool getProjectSearchInfoData:_keywords area:_city job:_job start:_start end:_end maxId:maxId success:^(ProjectListInfoRes *result) {
        [Utils refreshComplete:_tableView];
        if(result.isSucc){
            if(maxId == 0){
                [_projectArr removeAllObjects];
            }
            [_projectArr addObjectsFromArray:result.datas];
            [_tableView reloadData];
        }else{
          [DialogUtils showTipsWithView:self.view String:result.msg];
        }
    } failure:^(NSError *error) {
        [Utils refreshComplete:_tableView];
        [DialogUtils showTipsWithView:self.view String:@"加载失败"];
    }];
}

-(void)initTableView{
    [_tableView setDataSource:self];
    [_tableView setDelegate:self];
    [_tableView addHeaderWithTarget:self action:@selector(headerRefresh)];
    [_tableView addFooterWithTarget:self action:@selector(footerRefresh)];
    [_tableView setTableFooterView:[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 0)]];
}

-(void)headerRefresh{
    [self getDataWithMaxId:0];
}

-(void)footerRefresh{
    NSInteger maxId = 0;
    if (self.projectArr.count) {
        WorkReportItemResult *item = [self.projectArr lastObject];
        maxId = item.id;
    }
    if(maxId > 0){
        maxId =  maxId-1;
    }
    [self getDataWithMaxId:maxId];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _projectArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ProjectListCell *cell = [ProjectListCell cellWithTableView:tableView indexPath:indexPath];
    [cell initData:_projectArr[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ProjectListCellHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ProjectListDetailCtrl* ctrl = [ProjectListDetailCtrl initWithData:_projectArr[indexPath.row]];
    [self.navigationController pushViewController:ctrl animated:YES];
}

@end
